/**
 * 睡眠 延时
 * @param {Number} ms 需要延时的毫秒数
 */
export function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms))
}

/**
 * 合并对象，只合并原有对象中存在的参数，用法同 Object.assign()
 * @param {object} target 原对象
 * @param {object[]} sources 要合并覆盖的对象，可多个
 * @return {object} 合并的对象
 */
export function assignOverrideWithHas(target, ...sources) {
  sources.forEach((source) => {
    for (const key in source) {
      if (source.hasOwnProperty(key) && target.hasOwnProperty(key)) {
        target[key] = source[key]
      }
    }
  })
  return target
}

/**
 * 合并对象，只合并真值，用法同 Object.assign()
 * @param {object} target 原对象
 * @param {object[]} sources 要合并覆盖的对象，可多个
 * @return {object} 合并的对象
 */
export function assignOverrideWithTrue(target, ...sources) {
  sources.forEach((source) => {
    for (let key in source) {
      if (source.hasOwnProperty(key)) {
        if (source[key]) {
          target[key] = source[key]
        }
      }
    }
  })
  return target
}

/**
 * @description 获取当前时间对应的提示语
 * @returns {String}
 */
export function sayHello() {
  let timeNow = new Date()
  let hours = timeNow.getHours()
  if (hours >= 6 && hours <= 10) return `早上好 ⛅`
  if (hours >= 10 && hours <= 14) return `中午好 🌞`
  if (hours >= 14 && hours <= 18) return `下午好 🌞`
  if (hours >= 18 && hours <= 24) return `晚上好 🌛`
  if (hours >= 0 && hours <= 6) return `夜深啦，注意休息哦 🌛`
}

/**
   * 构建树形数据
   * @param {Array} list 要构建的扁平数组
   * @param {String} nameField name字段名，默认name
   * @param {String} parentField parent字段名，默认parent
   * @param {String} childrenField children字段名，默认children
   */
export function buildTree(list, nameField = "name", parentField = "parent", childrenField = "children") {
  const tree = []
  const treeMap = {}

  list.forEach((item) => {
    treeMap[item[nameField]] = item
    if (!item[parentField]) {
      tree.push(item)
    } else {
      const parentNode = treeMap[item[parentField]]
      if (parentNode) {
        parentNode[childrenField] = parentNode[childrenField] || []
        parentNode[childrenField].push(item)
      } else {
        tree.push(item) // 未查询到有效父级则默认放置根节点
      }
    }
  })

  return tree
}

/**
 * 将树形数组扁平化
 * @param {Array} tree 要扁平化的树形数组
 * @param {String} nameField name字段名，默认name
 * @param {String} parentField parent字段名，默认parent
 * @param {String} childrenField children字段名，默认children
 */
export function flattenTree(tree, nameField = "name", parentField = "parent", childrenField = "children") {
  const result = []

  function traverse(node, parentName) {
    // 复制节点属性并移除children字段
    const flattened = { ...node }
    delete flattened[childrenField]

    // 只有存在父级名称时，才设置parent字段
    if (parentName != null) {
      flattened[parentField] = parentName
    } else {
      // 确保根节点不保留parent字段
      delete flattened[parentField]
    }

    result.push(flattened)

    // 递归处理子节点
    const children = node[childrenField] || []
    for (const child of children) {
      traverse(child, node[nameField]) // 将当前节点name作为子节点的parent
    }
  }

  // 遍历每棵树的根节点
  for (const root of tree) {
    traverse(root, undefined) // 根节点没有父级
  }

  return result
}